package com.lx.repository;

import com.lx.entity.User;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 用户持久层
 *
 * @author 段誉
 * @create 2019-05-15 14:52
 */
public interface UserRepository extends JpaRepository<User, Integer> {

  /**
   * 根据用户名模糊查询
   * nativeQuery:是否使用原生sql，默认false，不使用原生sql查询的对象要用类名
   * @param name
   * @return java.util.List<com.lx.entity.User>
   * @author 段誉
   * @date 2019/5/20 14:18
   * @throws
   * @since 1.0.0
   */
  @Query(value = "SELECT tid, `name`, age FROM `user` WHERE INSTR(`name`, :name)", nativeQuery = true)
  List<User> findByNameLike(@Param("name") String name);

  /**
   * 根据用户名模糊查询(不在参数前加@Param注解)
   * nativeQuery:是否使用原生sql，默认false，不使用原生sql查询的对象要用类名
   * @param name
   * @return java.util.List<com.lx.entity.User>
   * @author 段誉
   * @date 2019/5/20 14:18
   * @throws
   * @since 1.0.0
   */
  @Query(value = "SELECT tid, `name`, age FROM `user` WHERE INSTR(`name`, ?1)", nativeQuery = true)
  List<User> findByNameLikeNoParam(String name);

  /**
   * @desc 分页查询
   * @Author 段誉
   * @Date 2019/5/20 15:12
   * @method
   */
  @Query(value = "SELECT tid, `name`, age FROM `user` WHERE INSTR(`name`, :name)",
          countQuery = "SELECT COUNT(tid) FROM `user` WHERE INSTR(`name`, :name)",
          nativeQuery = true)
  Page<User> findPageByNameLike(@Param("name") String name, Pageable pageable);

  /**
   * 根据用户名查询用户
   * @param username
   * @return com.lx.entity.User
   * @author 段誉
   * @date 2019/5/21 9:23
   * @throws
   * @since 1.0.0
   */
  User findOneByUsername(String username);
}
